package com.zhipu.balance.ui.vm.home.calculate.rice

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 谷外糙米
 */
class RiceBrownViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){

    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 稻谷实体
     */
    var mRecordRice : RecordRice? = null

    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 谷外糙米-试样质量
     */
    var qualityOfBrownRiceSample = ObservableField("")
    /**
     * 谷外糙米-糙米质量
     */
    var outsideBrownRiceQuality = ObservableField("")


    /**
     * 大样杂质率
     */
    var detailResults = ObservableField("")


    /**
     * 谷外糙米-结果
     */
    var brownRiceResult = ObservableField("")






    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordRice?.brownRiceRecorder)){
            //谷外糙米-试样质量
            qualityOfBrownRiceSample.set(mRecordRice?.qualityOfBrownRiceSample.toString())
            //谷外糙米-糙米质量
            outsideBrownRiceQuality.set(mRecordRice?.outsideBrownRiceQuality.toString())
            //大样杂质率
            detailResults.set(mRecordRice!!.detailResults.toString())
            //谷外糙米-结果
            brownRiceResult.set(mRecordRice!!.brownRiceResult.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //谷外糙米-试样质量
        qualityOfBrownRiceSample.set("")
        //谷外糙米-糙米质量
        outsideBrownRiceQuality.set("")
        //大样杂质率
        detailResults.set("")
        //谷外糙米-结果
        brownRiceResult.set("")
    }







    /**
     * 试样质量
     */
    fun BulkQuality(){
        qualityOfBrownRiceSample.set(weighing)
        mRecordRice?.qualityOfBrownRiceSample = qualityOfBrownRiceSample?.get()?.toDouble()
    }


    /**
     * 糙米质量
     */
    fun BrownRiceQuality(){
        outsideBrownRiceQuality.set(weighing)
        mRecordRice?.outsideBrownRiceQuality = outsideBrownRiceQuality?.get()?.toDouble()
    }

    /**
     * 大样杂质率
     */
    fun LargeImpurityRate(){
        detailResults.set(mRecordRice?.detailResults.toString())
        if(null != mRecordRice && mRecordRice?.smallMiscellaneousSampleQuality!! > 0){
            qualityOfBrownRiceSample.set(mRecordRice?.smallMiscellaneousSampleQuality.toString())
            mRecordRice?.qualityOfBrownRiceSample = qualityOfBrownRiceSample?.get()?.toDouble()
        }
    }


    /**
     * 计算
     */
    fun calculation(){
        if(null == mRecordRice){
            return
        }
        /**
         * 计算谷外糙米-结果
         */
        var mBrownRiceResult = CommonUtils.formatDouble((mRecordRice!!.outsideBrownRiceQuality / mRecordRice!!.qualityOfBrownRiceSample) * (100 - mRecordRice!!.detailResults))
        mRecordRice!!.brownRiceResult = mBrownRiceResult
        brownRiceResult.set(mBrownRiceResult.toString())
        /**
         * 谷外糙米记录员
         */
        mRecordRice!!.brownRiceRecorder = mPersonnel!!.name
        //试样质量
        if(mRecordRice!!.qualityOfBrownRiceSample == 0.00){
            qualityOfBrownRiceSample.set("0.00")
        }
        //糙米质量
        if(mRecordRice!!.outsideBrownRiceQuality == 0.00){
            outsideBrownRiceQuality.set("0.00")
        }
    }

}